import { MailOutlined, CalendarOutlined } from '@ant-design/icons-vue'
import { h } from 'vue'
import lodash from 'lodash'

import type { RouteType, SliderBarType } from './type'

/**
 * 常量路由
 * 自定义数据 meta: {
            hiddden?: true,//可选属性，是否隐藏,不加默认显示
            label: '首页', //必加属性
            icon: () => h(MailOutlined)//必加属性
        }
 */
export const constantRouter: RouteType[] = [
    {
        path: '/',
        name: '/',
        redirect: '/home',
        meta: {
            hidden: true
        }
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            hidden: true
        },
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/home',
        name: 'home',
        redirect: '/home/product-control',
        meta: {
            label: '数据管理',
            icon: () => h(MailOutlined)
        },
        component: () => import('@/views/home/index.vue'),
        children: [
            {
                path: '/home/product-control',
                name: 'product-control',
                meta: {
                    label: '农产品信息管理',
                    icon: () => h(MailOutlined)
                },
                component: () => import('@/views/homes/product-control/index.vue')
            }
        ]
    },
    {
        path: '/userinfo',
        name: 'userinfo',
        redirect: '/userinfo/edit-password',
        meta: {
            label: '个人中心',
            icon: () => h(MailOutlined)
        },
        component: () => import('@/views/home/index.vue'),
        children: [
            {
                path: '/userinfo/edit-password',
                name: 'edit-password',
                meta: {
                    label: '修改密码',
                    icon: () => h(MailOutlined)
                },
                component: () => import('@/views/homes/user-center/index.vue')
            },
            {
                path: '/userinfo/reset-password',
                name: 'reset-password',
                meta: {
                    label: '重置密码',
                    icon: () => h(MailOutlined)
                },
                component: () => import('@/views/homes/user-center/index.vue')
            },
            {
                path: '/userinfo/edit-info',
                name: 'edit-info',
                meta: {
                    label: '修改个人信息',
                    icon: () => h(MailOutlined)
                },
                component: () => import('@/views/homes/user-center/index.vue')
            }
        ]
    },
    {
        path: '/:catchAll(.*)',
        name: '404',
        meta: {
            hidden: true, // 隐藏
            label: '404',
            icon: () => h(MailOutlined)
        },
        component: () => import('@/views/blank/index.vue')
    }
]

const getSliderBars = (oldArr: RouteType[]) => {
    if (!Array.isArray(oldArr)) {
        throw new Error('传参 must be an array')
    }
    let arr = lodash.cloneDeep(oldArr) as RouteType[]
    let newarr: any[] = []
    arr.forEach((el: RouteType) => {
        if (el.children) {
            el.children = getSliderBars(el.children) //这里修改了实参对象，产生副作用，所以前边需要深拷贝，以免影响原对象
            newarr.push({
                key: el.path,
                label: el?.meta?.label,
                title: el.name,
                icon: el?.meta?.icon,
                path: el.path,
                children: el.children
            })
        } else {
            if (!el?.meta?.hidden) {
                newarr.push({
                    key: el.path, //这里设置成path为了防止出现菜单栏过多，出现重复选中的问题
                    label: el?.meta?.label,
                    title: el.name,
                    icon: el?.meta?.icon,
                    path: el.path
                })
            }
        }
    })
    return newarr
}

const DeepCopyObj = lodash.cloneDeep(constantRouter) as RouteType[] //函数内部已经实现是深拷贝，所以这里不需要再深拷贝

export const sliders = getSliderBars(DeepCopyObj) as SliderBarType[]
